﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace Hack.Misc
{
    public static class ScreenCapture
    {
        /// <summary>
        /// Author: Visual C# Kicks
        /// The code uses GDI in C#.NET to draw the primary screen onto a bitmap.
        /// Platform: .NET Framework 2.0
        /// </summary>
        public static class ScreenAPI
        {
            [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
            public static extern IntPtr GetDC(IntPtr hWnd);

            [DllImport("user32.dll", ExactSpelling = true)]
            public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

            [DllImport("gdi32.dll", ExactSpelling = true)]
            public static extern IntPtr BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);

            [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
            public static extern IntPtr GetDesktopWindow();
        }
        public static class ScreenShot
        {
            public static Bitmap Take()
            {
                int screenWidth = Screen.PrimaryScreen.Bounds.Width;
                int screenHeight = Screen.PrimaryScreen.Bounds.Height;

                Bitmap screenBmp = new Bitmap(screenWidth, screenHeight);
                Graphics g = Graphics.FromImage(screenBmp);

                IntPtr dc1 = ScreenAPI.GetDC(ScreenAPI.GetDesktopWindow());
                IntPtr dc2 = g.GetHdc();

                //Main drawing, copies the screen to the bitmap
                //last number is the copy constant
                ScreenAPI.BitBlt(dc2, 0, 0, screenWidth, screenHeight, dc1, 0, 0, 13369376);

                //Clean up
                ScreenAPI.ReleaseDC(ScreenAPI.GetDesktopWindow(), dc1);
                g.ReleaseHdc(dc2);
                g.Dispose();

                return screenBmp;
            }
            public static Bitmap Take(int x, int y, int width, int heigth)
            {
                int screenWidth = width;
                int screenHeight = heigth;

                Bitmap screenBmp = new Bitmap(screenWidth, screenHeight);
                Graphics g = Graphics.FromImage(screenBmp);

                IntPtr dc1 = ScreenAPI.GetDC(ScreenAPI.GetDesktopWindow());
                IntPtr dc2 = g.GetHdc();

                //Main drawing, copies the screen to the bitmap
                //last number is the copy constant
                ScreenAPI.BitBlt(dc2, x, y, screenWidth, screenHeight, dc1, 0, 0, 13369376);

                //Clean up
                ScreenAPI.ReleaseDC(ScreenAPI.GetDesktopWindow(), dc1);
                g.ReleaseHdc(dc2);
                g.Dispose();

                return screenBmp;
            }
        }
    }
}
